import QtQuick 2.0
import Interaction 1.0



Item{
    id:item;
    width: img.width
    height: img.height

    property int taskID: 0   // 在 ListModel中的任务ID
    property int gameTaskWidth:100
    property int endY:600
    property alias taskAnim:taskAnim
    property alias yAnim:yAnim
    property alias scaleAnim:scaleAnim
    property alias imgUrl:img.source
    property alias taskText:taskText.text

    // 该信号为将this和当前任务球的内容一起发送出去
    signal ineffectiveTask(int id)    // 任务球未成功消灭发出的信号


    Image{
        id:img;
        width: gameTaskWidth;
        source: 'qrc:/Images/orange.png'; //delete
        fillMode: Image.PreserveAspectFit;
    }

    Text {        
        id:taskText;
        text: "null"
        color: "white"
        font.bold: true
        font.pixelSize: 38;
        anchors.centerIn: parent;
    }

    ParallelAnimation{
        id:taskAnim
        PropertyAnimation{
            id:yAnim
            target: item
            property: 'y'
            from:item.y
            to:endY
            duration: 5000

        }
        RotationAnimation{
            id:rotationAnim
            target: img
            from:0
            to:360*5
            duration: 5000
        }
        onStopped: {
            // 如果 y值没有超过规定的endY那肯定就是中途被消灭了！
            if(item.y < endY) {
                scaleAnim.start();
                return;
            }

            ineffectiveTask(taskID);
            item.visible = false;
            this.destroy();
        }
    }

    PropertyAnimation{
        id:scaleAnim
        target:item
        property: 'scale'
        from:1.0
        to:0
        duration: 100
        onStopped: {
            var interaction = Qt.createQmlObject('import QtQuick 2.0;import Interaction 1.0; Interaction{}',item);

            img.source = interaction.getRandomEndImgUrl();
            taskText.text = ""
            endAnim.start();
        }
    }

    SequentialAnimation{
        id:endAnim
        PropertyAnimation{
            target:item
            property: 'scale'
            from:0
            to:1.0
            duration: 50
        }
        PropertyAnimation{
            target:item
            property: 'opacity'
            from:1.0
            to:0
            duration: 6000
        }
        onStopped: {
            this.destroy();
//            console.log("销毁成功了吧")
        }
    }

}


